<?php

declare(strict_types=1);
/**
 * This file is part of Hyperf.
 *
 * @link     https://www.hyperf.io
 * @document https://doc.hyperf.io
 * @contact  group@hyperf.io
 * @license  https://github.com/hyperf-cloud/hyperf/blob/master/LICENSE
 */

namespace App\Controller;
use Hyperf\DbConnection\Db;
use App\Utils\Log;

class IndexController extends AbstractController
{
    public function index()
    {
        $user = $this->request->input('user', 'Hyperf');
        $method = $this->request->getMethod();

        return [
            'method' => $method,
            'message' => "Hello {$user}.",
        ];
    }

    public function test()
    {
        return [
            'users' => time(),
        ];
    }

    public function login(){
        $name = $this->request->input('username');
        $password = $this->request->input('password');
        if (empty($name)) {
            return ['msg' => '用戶名不能为空！'];
        }
        if (empty($password)) {
            return ['msg' => '密码不能为空！'];
        }
        $userInfo = Db::table('users')
            ->where('username', '=', $name)
            ->first();
        if (empty($userInfo)) return ['msg' => '用户不存在！'];
        if ($password == $userInfo->password) {
            return [
                'name' => $name,
                'uuid' => $name,
                'token' => 123456,
                'msg' => '登录成功',
            ];
        } else {
            return [
                'msg' => '登录失败',
            ];
        }
    }

    public function menu()
    {
        $params = $this->request->all();
        Log::getInstance()->info('menu' ,$params);
        $routesList = Db::table('routes')->get()->toArray();
        foreach ($routesList as &$route){
            $route = object2array($route);
        }
        $treeList = child($routesList);
        return $treeList;
    }

    public function books()
    {
        $params = $this->request->all();
        Log::getInstance()->info('books' ,$params);
        $booksList = Db::table('books')->paginate($params['pageSize'] ?? 10);
        $booksList = $booksList->toArray();
        foreach ($booksList['data'] as &$book){
            $book = object2array($book);
            $book['date'] = date('Y-m-d',$book['publis_time']);
        }
        Log::getInstance()->info('books' ,$booksList);
        return $booksList;
    }

    public function books_add()
    {
        $params = $this->request->all();
        Log::getInstance()->info('books' ,$params);
        if (empty($params['title']) || empty($params['author']) || empty($params['pages']) || empty($params['publiser'])) {
            return ['code'=>500,'msg' => '参数不全！'];
        }
        $userInfo = Db::table('users')
            ->where('username', '=', $params['uuid'])
            ->first();
        if (empty($userInfo)) return ['msg' => '用户不存在！'];
        $insertData = [];
        $insertData[] = $params['title'];
        $insertData[] = $params['author'];
        $insertData[] = $params['pages'];
        $insertData[] = $params['publiser'];
        $insertData[] = time();
        $inserted = Db::insert('INSERT INTO books ( title,author,pages,publiser,publis_time) VALUES (?, ?,?,?,?)',$insertData);
        Log::getInstance()->info('books_add' ,['res'=>$inserted]);
        return ['msg' => '添加成功！'];
    }

    public function books_edit()
    {
        $params = $this->request->all();
        Log::getInstance()->info('books_edit_params' ,$params);
        if (empty($params['id']) || empty($params['title']) || empty($params['author']) || empty($params['pages']) || empty($params['publiser'])) {
            return ['code'=>500,'msg' => '参数不全！'];
        }
        $userInfo = Db::table('users')
            ->where('username', '=', $params['uuid'])
            ->first();
        if (empty($userInfo)) return ['msg' => '用户不存在！'];
        $updateData = [];
        $updateData['title'] = $params['title'];
        $updateData['author'] = $params['author'];
        $updateData['pages'] = $params['pages'];
        $updateData['publiser'] = $params['publiser'];
        $res = Db::table('books')->where('id','=',$params['id'])->update($updateData);
        Log::getInstance()->info('books_edit' ,['res'=>$res]);
        return ['msg' => '编辑成功！'];
    }

    public function books_del()
    {
        $params = $this->request->all();
        Log::getInstance()->info('books_del_params' ,$params);
        if (empty($params['id']) ) {
            return ['code'=>500,'msg' => 'id不能为空！'];
        }
        $res = Db::table('books')->where('id','=',$params['id'])->delete();;
        Log::getInstance()->info('books_del' ,['res'=>$res]);
        return ['msg' => '删除成功！'];
    }
}
